package com.buer.storage.service.impl;

import com.buer.storage.dao.SupplierDao;
import com.buer.storage.dto.SupplierDto;
import com.buer.storage.entity.Supplier;
import com.buer.storage.service.SupplierService;
import com.buer.storage.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    private SnowFlake snowFlake=new SnowFlake(2,3);
    @Resource
    private SupplierDao supplierDao;

    @Override
    public List<Supplier> findSupplier() {
        List<Supplier> supplier = supplierDao.findSupplier();
        return supplier;
    }

    @Override
    public int insert(SupplierDto supplierDto) {
        String orderNo=String.valueOf(snowFlake.nextId());
        supplierDto.setSupplierCode("SU" + orderNo);
        return supplierDao.insert(supplierDto);
    }

    @Override
    public int delete(int supplierId) {
        return supplierDao.delete(supplierId);
    }

    @Override
    public int update(Supplier supplier) {
        return supplierDao.update(supplier);
    }

    @Override
    public PageInfo<Supplier> findAllSupplier(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Supplier> list = supplierDao.findAllSupplier(map); // 获取数据
        PageInfo<Supplier> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<Supplier> searchSupplier(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Supplier> list = supplierDao.searchSupplier(map); // 获取数据
        PageInfo<Supplier> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public List<String> getAllSupplier() {
        return supplierDao.getAllSupplier();
    }

    @Override
    public Supplier getSupplierByName(String supplierName) {
        return supplierDao.getSupplierByName(supplierName);
    }
}
